package net.gxf.mail;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class SimpleRegistrationService {

	   private JavaMailSender mailSender;
	   private VelocityEngine velocityEngine;

	   public void setMailSender(JavaMailSender mailSender) {
	      this.mailSender = mailSender;
	   }

	   public void setVelocityEngine(VelocityEngine velocityEngine) {
	      this.velocityEngine = velocityEngine;
	   }

	   public void register(User user) {

	      // Do the registration logic...

	      sendConfirmationEmail(user);
	   }

	   private void sendConfirmationEmail(final User user) {
	      MimeMessagePreparator preparator = new MimeMessagePreparator() {
	         public void prepare(MimeMessage mimeMessage) throws Exception {
	            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
	            message.setTo(user.getEmailAddress());
	            message.setFrom(user.getEmailAddress()); // could be parameterized...
	            Map model = new HashMap();
	            model.put("user", user);
	            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "welcome.vm", model);
	            message.setText(text, true);
	         }
	      };
	      this.mailSender.send(preparator);
	   }
	}